//
//  WHYVisitorView.swift
//  WHYWeibo-1.0
//
//  Created by 王洪运 on 15/11/22.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit
import SnapKit

/// 访客视图的代理协议
//protocol WHYVisitorViewDelegate: NSObjectProtocol {
//    func visitorViewDidRegister()
//    func visitorViewDidLogin()
//    
//}

class WHYVisitorView: UIView {
    
//    MAR: - 代理
//    weak var delegate: WHYVisitorViewDelegate?
    
    
//    MAR: - 设置视图信息
    /// 设置访客视图信息
    func setupInfo(imageName imageName: String?, title: String) {
        messageLabel.text = title
        
        guard let imgName = imageName else {
            startAnimation()
            return
        }
        
        iconView.image = UIImage(named: imgName)
        
        homeIconView.hidden = true
        
        sendSubviewToBack(maskIconView)
    }
    
//    MARK: - 首页访客界面的动画效果
///   首页访客界面的动画效果
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        animation.removedOnCompletion = false
        
        iconView.layer.addAnimation(animation, forKey: nil)
    }

//    MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
//        防止此View从 SB 或 nib 创建
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    
//    MARK: - 懒加载子控件
    /// 图标
    private lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    // 遮罩
    private lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")

    // 小房子
    private lazy var homeIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    // 消息文字
    private lazy var messageLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜")
    
    lazy var registerButton = UIButton(title: "注册",
        backColor: UIColor.orangeColor(),
        fontSize: 14,
        backImageName: "common_button_white_disable")
    
    lazy var loginButton = UIButton(title: "登录",
        backColor: UIColor.darkGrayColor(),
        fontSize: 14,
        backImageName: "common_button_white_disable")
    

}

extension WHYVisitorView {
    /// 设置子控件的UI
    private func setupUI () {
        //添加子控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //设置子控件约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
        
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
//            make.size.equalTo(CGSizeMake(224, 36))
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
//        registerButton.addTarget(self, action: "clickRegisterButton", forControlEvents: .TouchUpInside)
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
//        loginButton.addTarget(self, action: "clickLoginButton", forControlEvents: .TouchUpInside)
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    
//    MAR: - 监听按钮点击方法
//    func clickRegisterButton() {
//        delegate?.visitorViewDidRegister()
//    }
//    
//    func clickLoginButton() {
//        delegate?.visitorViewDidLogin()
//    }
}
